import type {
  Block,
  CallExpression,
  ExpressionStatement,
  IfStatement,
  VariableStatement
} from 'typescript';
import { conditional, simpleProgram } from './fixtures';

import { tsquery } from '../src/index';

describe('tsquery:', () => {
  describe('tsquery - field:', () => {
    it('should find any nodes with a single field', () => {
      const ast = tsquery.ast(conditional);
      const result = tsquery(ast, '.expression');

      expect(result).toEqual([
        (ast.statements[0] as IfStatement).expression,
        (
          ((ast.statements[0] as IfStatement).thenStatement as Block)
            .statements[0] as ExpressionStatement
        ).expression,
        (
          (
            ((ast.statements[0] as IfStatement).thenStatement as Block)
              .statements[0] as ExpressionStatement
          ).expression as CallExpression
        ).expression,
        (
          ((ast.statements[0] as IfStatement).elseStatement as Block)
            .statements[0] as ExpressionStatement
        ).expression,
        (ast.statements[1] as IfStatement).expression,
        (
          ((ast.statements[1] as IfStatement).thenStatement as Block)
            .statements[0] as ExpressionStatement
        ).expression,
        ((ast.statements[1] as IfStatement).elseStatement as IfStatement)
          .expression,
        (
          (
            ((ast.statements[1] as IfStatement).elseStatement as IfStatement)
              .thenStatement as Block
          ).statements[0] as ExpressionStatement
        ).expression
      ]);
    });

    it('should find any nodes with a field sequence', () => {
      const ast = tsquery.ast(simpleProgram);
      const result = tsquery(ast, '.declarations.initializer');

      expect(result).toEqual([
        (ast.statements[0] as VariableStatement).declarationList.declarations[0]
          .initializer,
        (ast.statements[1] as VariableStatement).declarationList.declarations[0]
          .initializer
      ]);
    });
  });
});
